﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Elkitools.EditorParts.ListSelector.Tools;

namespace Elkitools.EditorParts.ListSelector.Controls
{
    /// <summary>
    /// This class is to be used to store OffLine data on a SPSite
    /// </summary>
    [Serializable]
    internal class CalendarWebData
    {
        //-------------------------------------------------------------------------
        #region Properties
        //-------------------------------------------------------------------------

        /// <summary>
        /// Title or Name of the SPSite
        /// </summary>
        internal string Title { get; set; }

        /// <summary>
        /// Server-relative Url of the SPSite
        /// </summary>
        internal string ServerRelativeUrl { get; set; }

        /// <summary>
        /// List of SPLists of type Calendar
        /// </summary>
        internal IList<CalendarListData> ListsData { get; set; }

        /// <summary>
        /// List of sub SPWebs
        /// </summary>
        internal IList<CalendarWebData> WebsData { get; set; }

        //-------------------------------------------------------------------------
        #endregion Properties
        //-------------------------------------------------------------------------

        //-------------------------------------------------------------------------
        #region Constructor & initializations
        //-------------------------------------------------------------------------

        /// <summary>
        /// Default Constructor
        /// </summary>
        internal CalendarWebData()
        {
            this.ListsData = new List<CalendarListData>();
        }

        /// <summary>
        /// Constructor that takes the refence SPWeb as parameter
        /// </summary>
        /// <param name="pobjWeb"></param>
        internal CalendarWebData(SPWeb pobjWeb) : this()
        {
            InitializeData(pobjWeb);
        }

        /// <summary>
        /// Initilise internal data
        /// </summary>
        /// <param name="pobjWeb"></param>
        private void InitializeData(SPWeb pobjWeb)
        {
            // Get basic informations
            try
            {
                if (!string.IsNullOrEmpty(pobjWeb.Title))
                {
                    this.Title = pobjWeb.Title;
                }
                else
                {
                    this.Title = pobjWeb.Name;
                }
                this.ServerRelativeUrl = pobjWeb.ServerRelativeUrl;
            }
            catch (Exception excBasicData)
            {
                UlsLogger.Error("An error occured while initializing basic informations in CalendarWebData : " + excBasicData.Message);
            }

            // Enumerate webs and fetch them to subWebs list
            try
            {
                WebsData = (from SPWeb subWeb in pobjWeb.Webs
                            select new CalendarWebData(subWeb))
                           .ToList();
            }
            catch (Exception excGetWebs)
            {
                UlsLogger.Error("An error occured while initializing subwebs informations in CalendarWebData : " + excGetWebs.Message);
            }

            // Enumerate lists and fetch them to Calendar Lists
            try
            {
                this.ListsData =
                    (from SPList list in pobjWeb.GetListsOfType(SPBaseType.GenericList)
                     where list.BaseTemplate == SPListTemplateType.Events
                     select new CalendarListData(list))
                    .ToList();
            }
            catch (Exception excListsData)
            {
                UlsLogger.Error("An error occured while initializing lists informations in CalendarWebData : " + excListsData.Message);
            }
        }

        //-------------------------------------------------------------------------
        #endregion Constructor & initializations
        //-------------------------------------------------------------------------
    }
}
